limsDbconn.testSQLPage = {

    sqlId : null,

    getForm : function(){
        var _this = this;
        return {
          type : 'form',
          id : 'formTest',
          columns : 4,
          titleWidth : 80,
          fields : [
              {
                   field : 'sqlId',
                   title : 'SQL',
                   type: 'choose',
                   validators : ['notEmpty'],
                   category: 'dbExtractConfig',
                   onBeforeChoose: function () {
                       return {
                           single: true,
                       }
                   },
                   onAfterChoose: function (rows, index) {
                       if (Gikam.isNotEmpty(rows)) {
                           //生成参数填写框
                           _this.sqlId = rows[0].id;
                            var basicFields = [{
                                field : 'sql',
                                title : 'SQL',
                                value : rows[0].sql,
                                readonly : true,
                                colspan : 3,
                            },{
                                field : 'args',
                                title : '参数',
                                value : rows[0].args,
                                readonly : true,
                            }];
                           var argFields = [];
                           if(Gikam.isNotEmpty(rows[0].args)){
                                 argFields = rows[0].args.split(',').map(function(value) {
                                      return {
                                          field : 'arg:'+value,
                                          title : value,
                                      }
                                  });
                           }
                           if(argFields.length>0){
                                basicFields = basicFields.concat(argFields);
                           }
                           this.refreshFields(basicFields);
                       }
                   },
              },
          ]
      };
    },

    create : function() {
        var _this = this;
        Gikam.create('layout', {
            id : 'lims-testsql-modal-list-layout',
            renderTo : Gikam.getLastModal().window.$dom,
            center : {
                items : [ this.getForm() ]
            },
            south : {
                height : '60%',
                title : '结果（只展示10条记录）',
                items : [{
                      id : 'resultGrid',
                      type : 'grid',
                      columns : [],
                      data : [],
                      toolbar : [ {
                          type : 'button',
                          text : '查询',
                          icon : 'select',
                          onClick : function() {
                                var form = Gikam.getComp('formTest');
                                 if (Gikam.isFalse(form.validate())) {
                                     return;
                                 }
                                 workspace.window.showMask();
                                 var p = form.getData();
                                 p.dbconnectId = _this.param.dbconnectId;
                                 p.sqlId = _this.sqlId;
                                 Gikam.post(limsDbconn.baseUrl+'/testSQL', Gikam.getJsonWrapper(p, ['', []])).done(function(data) {
                                      // 返回数据给前台展示
                                        var columns = [{
                                            index : true,
                                        }];
                                        if(Gikam.isNotEmpty(data)){
                                            var d = data[0];
                                            var keys = Object.keys(d);
                                            for(var i=0;i<keys.length;i++){
                                                columns.push({
                                                    title : keys[i],
                                                    field : keys[i],
                                                });
                                            }
                                        }
                                        Gikam.getComp('resultGrid').refreshColumns(columns);
                                        Gikam.getComp('resultGrid').loadData(data);
                                 }).always(function () {
                                     workspace.window.closeMask();
                                 });
                          }
                      }]
                   }
                ]
            },
        });
    },

    init : function(param) {
        this.param = param;
        this.create();
    }
};
